package com.edu.admin.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysRoleMenu;
import com.edu.admin.mapper.RoleMenuMapper;
import com.edu.admin.vo.RoleMenuVo;

/**
 * 角色菜单服务
 *
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class RoleMenuService extends BaseService<RoleMenuMapper, SysRoleMenu> {
	
	/**
	 * 获取角色菜单
	 * 
	 * @param roleId
	 * @param menuId
	 * 
	 * @return SysRoleMenu
	 */
	public SysRoleMenu getRoleMenuByRoleIdAndMenuId(Long roleId,Long menuId){
		return this.mapper.getRoleMenuByRoleIdAndMenuId(roleId,menuId);
	}
	/**
	 * 获取角色菜单列表
	 * 
	 * @param menuId
	 * 
	 * @return List<RoleMenuVo>
	 */
	public List<RoleMenuVo> getRoleMenuListByMenuId(Long menuId) {
		return this.mapper.getRoleMenuListByMenuId(menuId);
	}
	
	/**
	 * 获取角色菜单列表
	 * 
	 * @param roleId
	 * @return List<RoleMenuVo>
	 */
	public List<RoleMenuVo> getRoleMenuListByRoleId(Long roleId) {
		return this.mapper.getRoleMenuListByRoleId(roleId);
	}
	
	/**
	 * 删除角色下菜单
	 * 
	 * @param roleId
	 * @return
	 */
	public void deleteRoleMenuByRoleId(Long roleId) {
		this.mapper.deleteRoleMenuByRoleId(roleId);
	}
	
	
	/**
	 * 角色菜单保存
	 * 
	 * @param Long roleId
	 * @param Integer[] menuIds
	 * 
	 * @return
	 */
	public void saveRoleMenu(Long roleId, Integer[] menuIds) {
		
		SysRoleMenu roleMenu = null;
		
		deleteRoleMenuByRoleId(roleId);
		
		for (int i=0;i<menuIds.length;i++)
		{
			roleMenu = new SysRoleMenu();
			roleMenu.setRoleId(roleId);
			roleMenu.setMenuId(menuIds[i].longValue());
			
			insert(roleMenu);
		}
		
	}
	
	/**
	 * 角色菜单保存
	 * 
	 * @param SysRoleMenu
	 * 
	 * @return
	 */
	public void saveRoleMenu(SysRoleMenu roleMenu) {
		
		Long roleId = roleMenu.getRoleId();
		Long menuId = roleMenu.getMenuId();
		
		
		SysRoleMenu srm = getRoleMenuByRoleIdAndMenuId(roleId,menuId);

		if (srm == null) {
			
			insert(roleMenu);

		} else {
			updateByPrimaryKeySelective(roleMenu);

		}

	}
	
	/**
	 * 获取角色所有权限URL
	 * 
	 * @param roleId
	 * @return String
	 */
	public String getUrlsByRoleId(Long roleId) {
		if (roleId==null) {
			return "";
		}
		
		RoleMenuVo roleMenu = this.mapper.getUrlsByRoleId(roleId);
		if (roleMenu!=null)
		{
			return roleMenu.getRequestUrls();
		}
		return "";
	}
	
	/**
	 * 批量保存角色菜单
	 * 
	 * @param roleId
	 *
	 */
	public void batchSave(List<SysRoleMenu> roleMenuList){
		this.mapper.batchSave(roleMenuList);
	}
	

}
